package com.common.helper;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.ImageView;

import com.common.StarAppConst;
import com.starappallcommon.R;

/**
 * 绘制带文字头像类 <br>
 * Created 2014-10-30 下午3:22:39
 * 
 * @version
 * @author gpy
 * 
 * @see
 * 
 *      Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 * 
 */

public class DrawDefaultHead {
    /** 实例 */
    private static DrawDefaultHead instance = new DrawDefaultHead();
    /** 昵称头字 */
    private String mNickNameHead;
    /** 上下文 */
    private Context mContext;
    /**头像 */
    private ImageView mIvHead;

    /**
     * 获取实例 <br>
     * Created 2014-10-30 下午3:23:48
     * 
     * @return DrawDefaultHead实例
     * @author gpy
     */
    public static DrawDefaultHead getInstance() {
        if (instance == null) {
            instance = new DrawDefaultHead();
        }
        return instance;
    }

    /**
     * 
     * <br>
     * Created 2014-10-30 下午2:29:40
     * 
     * @param context
     *            上下文
     * @param strNickName
     *            昵称
     * @param ivHead
     *            所在ImageView
     * @author gpy
     */
    public void drawHeadName(Context context, String strNickName, ImageView ivHead) {
        if (TextUtils.isEmpty(strNickName) || null == ivHead) {
            return;
        }
        mNickNameHead = strNickName.substring(0, 1);
        mContext = context;
        mIvHead = ivHead;
        new HeadHandler().sendEmptyMessage(0);

    }

    /**
     * 文字头像生成线程
     * <br>Created 2014-11-3 上午9:32:33
     * @version  DrawDefaultHead
     * @author   gpy		
     *
     * @see 	 
     * 
     * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
     * 
     */
    
    
    class HeadHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            InputStream is = mContext.getResources().openRawResource(R.drawable.default_head);
            Bitmap bitmap = BitmapFactory.decodeStream(is).copy(Bitmap.Config.ARGB_8888, true);
            Canvas canvas = new Canvas(bitmap);// 初始化画布

            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);// 创建画笔
            paint.setTextSize(100.0f);// 设置文字的大小
            paint.setColor(Color.WHITE);// 文字的颜色
            paint.setTypeface(StarAppConst.getInstance().getLTXTypeface());
            // 根据文字计算坐标(此为单文字计算方法，如果是字符串用FontMetricsInt计方便)
            Rect textBounds = new Rect();
            paint.getTextBounds(mNickNameHead, 0, mNickNameHead.length(), textBounds);
            int tbBeft = textBounds.left;
            int textWidth = textBounds.right - tbBeft;
            // x,y即为文字坐标系的远点坐标
            // 文字长为bottom-top, 此时文字中心距为bottom-半长，因为为负坐标，取绝对值，即|bottom-(bottom-top)/2|
            int textHeight = Math.abs(textBounds.bottom + textBounds.top);
            int x = (bitmap.getWidth() - textWidth) / 2;
            if (tbBeft > 1) {// 一般中文left从1开始，逗号之类小标点left会大些，所以居中要剪掉这部分left
                x = x - tbBeft;
            }
            if (mNickNameHead.equals("1")) {// 数字1特殊情况，额外距离再手动做下调整
                x = x - textWidth / 4;
            }
            int y = bitmap.getHeight() / 2 + textHeight / 2;

            canvas.drawText(mNickNameHead, x, y, paint);// 将文字写入
            canvas.save(Canvas.ALL_SAVE_FLAG);// 保存所有图层
            canvas.restore();

            mIvHead.setImageBitmap(bitmap);
        }
    }
    
    
}
